//
//  WPTopupTopAlertView.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/10.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPTopupTopAlertView.h"
@interface WPTopupTopAlertView ()
@property (nonatomic, strong) UIView * grayView;/**< 灰色背景 */
@property (nonatomic, strong) UIImageView * imageView;/**< 账户余额图标 */
@property (nonatomic, strong) UILabel * balanceTitleLabel;/**< 余额标题 */
@property (nonatomic, strong) UITextField * balanceTextField;/**< 余额显示 */
@property (nonatomic, strong) NSString * balance;/**< 余额 */
@property (nonatomic, strong) UILabel * topLabel;/**< 提醒文本 */
@property (nonatomic, strong) UIButton * topupBtn;/**< 充值按钮 */
@property (nonatomic, copy) void (^complete)(NSString *result);/**< 结果block */
- (void)initializeAppearance;/**< 一般界面初始化 */


@end
@implementation WPTopupTopAlertView

+ (WPTopupTopAlertView *)showViewWithSize:(CGRect)rect andWithTitle:(NSString *)title andWithBalance:(NSString *)balance WithComplete:(void (^)(NSString *))complete {
    WPTopupTopAlertView * view = [[WPTopupTopAlertView alloc]initWithTitle:title withSize:rect andWithBalance:balance WithComplete:complete];
    [view show];
    return view;
}

- (instancetype)initWithTitle:(NSString *)title withSize:(CGRect)rect andWithBalance:(NSString *) balance WithComplete:(void (^)(NSString *))complete {
    self = [super initWithFrame:rect withTitle:title];
    if (self) {
        _balance = balance;
        _complete = complete;
        [self initializeAppearance];
    }
    return self;
}
#pragma mark - init
- (void)initializeAppearance {
    [self addSubview:self.grayView];
    [self addSubview:self.topLabel];
    [self addSubview:self.topupBtn];
}
#pragma mark - responds events
- (void)respondsToTopupBtn {
    [self hide];
    _complete(@"充值");
}
#pragma mark - getter
- (UIView *)grayView {
    if (!_grayView) {
        _grayView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 40*DHFlexibleHorizontalMutiplier()+2, CGRectGetWidth(self.bounds), 60*DHFlexibleHorizontalMutiplier())];
            view.backgroundColor = COLOR_RGB(233, 233, 233, 1);
            [view addSubview:self.imageView];
            [view addSubview:self.balanceTitleLabel];
            [view addSubview:self.balanceTextField];
            view;
        });
    }
    return _grayView;
}
- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = ({
            UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(15*DHFlexibleHorizontalMutiplier(), 10*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier(), 35*DHFlexibleHorizontalMutiplier())];
            view.image = IMAGE_NAME(@"zhyetb.png");
            view;
        });
    }
    return _imageView;
}
- (UILabel *)balanceTitleLabel {
    if (!_balanceTitleLabel) {
        _balanceTitleLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(65*DHFlexibleHorizontalMutiplier(), CGRectGetMinY(self.imageView.frame), 200, 20*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(61, 61, 61, 1);
            lab.text = @"账户余额";
            lab;
        });
    }
    return _balanceTitleLabel;
}
- (UITextField *)balanceTextField {
    if (!_balanceTextField) {
        _balanceTextField = ({
            UITextField * text = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.balanceTitleLabel.frame), CGRectGetMaxY(self.balanceTitleLabel.frame), 200, 20*DHFlexibleHorizontalMutiplier())];
            text.leftViewMode = UITextFieldViewModeAlways;
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60*DHFlexibleHorizontalMutiplier(), 15*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(187, 187, 187, 1);
            lab.text = @"账户余额：";
            text.leftView = lab;
            text.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            text.textColor = COLOR_RGB(187, 187, 187, 1);
            text.userInteractionEnabled = NO;
            text.text = _balance;
            text;
        });
    }
    return _balanceTextField;
}
- (UILabel *)topLabel {
    if (!_topLabel) {
        _topLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(self.imageView.frame), CGRectGetMaxY(self.grayView.frame), 200, 40*DHFlexibleHorizontalMutiplier())];
            lab.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            lab.textColor = COLOR_RGB(236, 82, 97, 1);
            lab.text = @"账户余额不足，请充值";
            lab;
        });
    }
    return _topLabel;
}
- (UIButton *)topupBtn {
    if (!_topupBtn) {
        _topupBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(50*DHFlexibleHorizontalMutiplier(), CGRectGetMaxY(self.topLabel.frame), 170*DHFlexibleHorizontalMutiplier(), 30*DHFlexibleHorizontalMutiplier());
            btn.backgroundColor = COLOR_RGB(234, 27, 47, 1);
            btn.layer.cornerRadius = 5*DHFlexibleHorizontalMutiplier();
            [btn setTitle:@"立即充值" forState:UIControlStateNormal];
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:14*DHFlexibleHorizontalMutiplier()];
            [btn addTarget:self action:@selector(respondsToTopupBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _topupBtn;
}
@end
